/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.composite.camel.processor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import se.sll.service.catalogue.offers.api.model.OfferUnitList;
import se.sll.service.catalogue.offers.api.model.Unit;
import se.sll.service.catalogue.offers.api.model.UnitList;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class OfferProcessor {

    /**
     * 
     * @param exchange
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException 
     */
    public void offerResponseJSON(Exchange exchange) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<OfferUnitList> responses = mapper.readValue(exchange.getIn().getBody(String.class), new TypeReference<List<OfferUnitList>>() {
        });
        exchange.setProperty("offer", responses);
        exchange.getIn().setBody(responses);
    }

    /**
     * Helper method to build a request list object with unit id's
     * @param exchange 
     */
    @SuppressWarnings("unchecked")
    public void listToOfferModel(Exchange exchange) {
        UnitList l = new UnitList();
        List<Unit> lu = new ArrayList<>();
        List<String> hsaIdList = (List<String>) exchange.getIn().getBody();
        for (String a : hsaIdList) {
            Unit u = new Unit();
            u.setUnitId(a);
            lu.add(u);
        }
        l.setUnits(lu);
        exchange.getIn().setBody(l);
    }
}
